package com.justnow;


import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * @author justnow
 * Created on 2020-09-27
 * Description
 */
public class Wy02_2 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        int T = Integer.parseInt(str);

        for (int i = 0; i < T; i++) {
            String s1 = scanner.nextLine();
            int N = Integer.parseInt(s1);

            boolean[] list = new boolean[1000001];
            Map<Integer, String> map =  new HashMap<>();
            for (int j = 0; j < N; j++) {
                String input = scanner.nextLine();
                String[] s = input.split(" ");

                if (s[0].equals("open")) {
                    String name = s[1];
                    int count = -1;
                    int k;
                    for (k = 0; k < list.length; k++) {
                        if (list[k] == false) {
                            list[k] = true;
                            break;
                        }
                    }
                    System.out.println(k);

                    map.put(k, name);
                    continue;
                }

                if (s[0].equals("dup")) {

                    int fd = Integer.parseInt(s[1]);
                    String name = map.get(fd);
                    int k;
                    for (k = 0; k < list.length; k++) {
                        if (list[k] == false) {
                            list[k] = true;
                            break;
                        }
                    }
                    System.out.println(k);
                    map.put(k, name);
                    continue;
                }

                if (s[0].equals("dup2")) {
                    int fd = Integer.parseInt(s[1]);
                    int fd_new = Integer.parseInt(s[2]);
                    String name = map.get(fd);
                    list[fd_new] = true;
                    map.put(fd_new, name);
                    continue;
                }
                if (s[0].equals("close")) {
                    int fd = Integer.parseInt(s[1]);
                    list[fd] = false;
                    map.remove(fd);
                    continue;
                }
                if (s[0].equals("query")) {
                    int fd = Integer.parseInt(s[1]);
                    System.out.println(map.get(fd));
                }
            }
        }
    }
}

